<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/28 00:39
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Address_Model extends WX_Model
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取列表
     * @param $userId
     * @return array
     */
    public function get($userId)
    {
        $query = $this->db->order_by('created', 'DESC')->get_where('user_address', array(
            'userId' => $userId,
            'status' => 1
        ));
        $rows = $query->result_array();

        if (empty($rows)) {
            return array();
        } else {
            $rows = $this->afterSelect($rows);
            return $rows;
        }

        return $rows;
    }

    /**
     * 添加
     * @param array $data
     * @return array|bool
     */
    public function add(Array $data)
    {
        if (!empty($data['default'])) {
            $db = clone($this->db);
            $db->where('userId', $data['userId']);
            $db->update('user_address', ['default' => 0]);
        }

        $this->db->insert('user_address', $data);
        $lastId = $this->db->insert_id();

        if ($lastId) {
            $data['userId'] = intval($data['userId']);
            $data['default'] = !empty($data['default']) ? true : false;
            return array_merge($data, array('id' => intval($lastId)));
        }
        return false;
    }

    /**
     * 编辑地址
     * @param array $data
     * @return bool
     */
    public function edit(Array $data)
    {
        if (!empty($data['default'])) {
            $db = clone($this->db);
            $db->where('id', $data['addressId']);
            $db->update('user_address', ['default' => 0]);
        }

        $this->db->where(['id' => $data['addressId']]);
        unset($data['addressId']);
        $this->db->update('user_address', $data);

        $affectedRows = $this->db->affected_rows();
        if ($affectedRows) {
            return true;
        }
        return false;
    }

    /**
     * 设置默认
     * @param $userid
     * @param $addressId
     * @return mixed
     */
    public function setDefault($userid, $addressId)
    {
        $db = clone($this->db);
        $db->where('userId', $userid);
        $db->update('user_address', ['default' => 0]);

        $upDb = clone($this->db);
        $upDb->where(['userId' => $userid, 'id' => $addressId]);
        $upDb->update('user_address', ['default' => 1]);

        return $upDb->affected_rows();
    }

    /**
     * 删除条目
     * @param $userid
     * @param $addressId
     * @return mixed
     */
    public function deleteItem($userid, $addressId)
    {
        $db = clone($this->db);
        $db->where(['userId' => $userid, 'id' => $addressId]);
        $db->update('user_address', ['status' => -1]);
        return $db->affected_rows();
    }

    /**
     * 地址列表
     * @param $rows
     * @return array
     */
    public function afterSelect($rows)
    {
        $array = array();
        if (!empty($rows) && is_array($rows)) foreach ($rows as $row) {
            $array[] = array(
                'addressId' => (int)$row['id'],
                'name' => value($row, 'name'),
                'phone' => value($row, 'phone'),
                'province' => value($row, 'province'),
                'city' => value($row, 'city'),
                'area' => value($row, 'area'),
                'address' => value($row, 'address'),
                'created' => value($row, 'created'),
                'default' => !empty($row['default'])
            );
        }
        return $array;
    }

    /**
     * 获取一条地址,如果是默认的则选择默认的,不是则最新的
     * @param $userid
     * @return bool
     */
    public function getOne($userid)
    {
        $db = clone($this->db);
        $q = $db->get_where('user_address', ['userId'=> $userid, 'default' => 1, 'status' => 1]);
        $row = $q->row_array();
        if (!empty($row)) {
            return $row;
        }

        $q = $db->order_by('created', 'DESC')->get_where('user_address', ['userId' => $userid, 'status' => 1]);
        $row = $q->row_array();
        if (!empty($row)) {
            return $row;
        }

        return false;
    }

    /**
     * 一条详情
     * @param $itemId
     * @return bool
     */
    public function getItem($itemId)
    {
        $db = clone($this->db);
        $q = $db->get_where('user_address', ['id'=> $itemId]);
        $row = $q->row_array();
        if (empty($row)) {
            return false;
        }

        return $row;
    }
}